package com.dtborad.myblog.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtborad.myblog.common.CommonErrorCode;
import com.dtborad.myblog.common.Result;
import com.dtborad.myblog.entity.Likes;
import com.dtborad.myblog.entity.Posts;
import com.dtborad.myblog.service.LikeService;
import com.dtborad.myblog.service.PostService;
import com.dtborad.myblog.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/like")
@Slf4j
@Api(tags = "点赞模块")
public class LikeController {

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    //点赞
    @PostMapping("/add")
    public Result<Void> like(@RequestBody Likes likes) {
        try{
            LambdaQueryWrapper<Posts> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Posts::getPostid, likes.getPostid());
            Posts one = postService.getOne(wrapper);
            if(one != null){
                //查询是否已经点过赞了
                LambdaQueryWrapper<Likes> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Likes::getPostid, one.getPostid()).eq(Likes::getUserid, likes.getUserid());
                Likes one1 = likeService.getOne(lqw);
                if(one1 != null){
                    return Result.ofFail(CommonErrorCode.FAILURE);
                }
                Likes like = new Likes();
                like.setPostid(one.getPostid());
                like.setUserid(likes.getUserid());
                likeService.save(like);
                //更新文章表的点赞量
                one.setLikes(one.getLikes() + 1);
                //postService.updatePostById(one);
                postService.updateById(one);
            }
        }catch (Exception e){
            return Result.ofFail(CommonErrorCode.INVALID_PARAM);
        }
        return Result.ofSuccess();
    }

    //查询点赞状态
    @PostMapping("/query")
    public Result<Boolean> queryLikeStatus(@RequestBody Likes likes){
        try{
            LambdaQueryWrapper<Likes> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Likes::getPostid, likes.getPostid()).eq(Likes::getUserid, likes.getUserid());
            Likes one = likeService.getOne(wrapper);
            if(one != null){
                return Result.ofFail("1001", "已点赞");
            }else{
                return Result.ofSuccess(true);
            }
        }catch (Exception e){
            return Result.ofFail(CommonErrorCode.FAILURE);
        }
    }

    //我的喜欢
    @GetMapping("/list")
    public Result<Map<String, Object>> list(@RequestParam(defaultValue = "1") int pagenum,
                                            @RequestParam(defaultValue = "30") int pagesize,
                                            @RequestParam(defaultValue = "") Integer categoryid,
                                            @RequestParam(defaultValue = "") String content,
                                            @RequestParam(defaultValue = "") Integer usersid){
        Page<Posts> currentPage = postService.getAllPostsByPageSize(pagenum, pagesize, categoryid, content, null);
        Map<String, Object> response = new HashMap<>();
        List<Posts> records = currentPage.getRecords();
        //仅保留点赞项
        LambdaQueryWrapper<Likes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Likes::getUserid, usersid);
        List<Likes> list = likeService.list(wrapper);
        List<Posts> likedPosts = records.stream()
                // 使用 anyMatch 方法判断文章是否被用户点赞过
                .filter(post -> list.stream().anyMatch(like -> like.getPostid().equals(post.getPostid())))
                .collect(Collectors.toList());
        //修改total的值
        long total = likedPosts.size();
        response.put("records", likedPosts);
        response.put("total", total);
        return Result.ofSuccess(response);
    }

    //取消点赞
    @PostMapping("/cancel")
    public Result<Void> cancelLike(@RequestBody Likes likes){
        try{
            LambdaQueryWrapper<Likes> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Likes::getPostid, likes.getPostid()).eq(Likes::getUserid, likes.getUserid());
            Likes one = likeService.getOne(wrapper);
            if(one != null){
                likeService.remove(wrapper);
                //更新文章点赞数
                LambdaQueryWrapper<Posts> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Posts::getPostid, likes.getPostid());
                Posts post = postService.getOne(lqw);
                post.setLikes(post.getLikes() - 1);
                //postService.updatePostById(post);
                postService.updateById(post);
            }else{
                return Result.ofFail(CommonErrorCode.DATA_NOT_FOUND);
            }
        }catch (Exception e){
            return Result.ofFail(CommonErrorCode.FAILURE);
        }
        return Result.ofSuccess();
    }
}
